
#VERTEX_SHADER
#version    330

layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texCoord;
layout(location = 2) in vec3 normal;

out vec2 TexCoord;
out vec3 Normal;
out vec3 FragPos;


uniform mat4 view;
uniform mat4 projection;
uniform mat4 model;

void main() {
    gl_Position = projection * view * model * position;
    Normal = mat3(transpose(inverse(model))) * normal;
    FragPos = vec3(model * position);
    TexCoord = texCoord;
}


#FRAGMENT_SHADER
#version    330

out vec4 color;
in vec2 TexCoord;
in vec3 Normal;
in vec3 FragPos;
uniform sampler2D diffuse;
uniform vec3 viewPos;

//平行光
struct DirLight {
	vec3 direction;
	
	vec3 ambient;
	vec3 diffuse;
	vec3 specular;
};
uniform DirLight sun;

vec3 DirLightHandler(DirLight light, vec3 norm, vec3 viewDir);

void main() {
    vec3 tem_color = vec3(texture(diffuse,vec2(TexCoord.x,1-TexCoord.y)));
    if (tem_color == vec3(1,0,1)) {
        discard;
    }
    color = vec4(tem_color,1);
    vec3 norm = normalize(Normal);
    vec3 viewDir = normalize(FragPos - viewPos);
    color = vec4(DirLightHandler(sun,norm,viewDir),1);
}

vec3 DirLightHandler(DirLight light, vec3 norm, vec3 viewDir)
{
	vec3 texDiffuseColor = vec3(texture(diffuse,vec2(TexCoord.x,1-TexCoord.y)));
	//环境光
	vec3 ambient = light.ambient * texDiffuseColor;
	//漫反射
	vec3 nlightDir = normalize(-light.direction);
	vec3 diffuse = light.diffuse * max( dot(nlightDir,norm), 0.0f) * texDiffuseColor;
	//高光
	vec3 reflectDir = normalize(reflect(-light.direction, norm));
	float spec = pow(max(dot(reflectDir, viewDir), 0.0f), 64);
	//vec3 specular = light.specular * spec * vec3(texture(diffuse,vec2(TexCoord.x,1-TexCoord.y)));
    vec3 specular = light.specular * spec * texDiffuseColor;
	return ambient + diffuse + specular;
}

